package org.grow.took.Controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.grow.took.dao.DealBillDao;
import org.grow.took.dao.PlayerDao;
import org.grow.took.entity.*;
import org.grow.took.entity.enums.DealState;
import org.grow.took.exception.SeptemberException;
import org.grow.took.service.PlayerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

//处理器的统一异常处理
@RestController
@RequestMapping("/ceto/dealBill")
public class DealBillController {
    @Autowired
    private DealBillDao dealBillDao;
    @Autowired
    private PlayerDao playerDao;
    @Autowired
    private PlayerService playerService;
    @PostMapping("/insert")
    public Boolean insert(@RequestBody DealBill dealBill){
        dealBill.setDealBillId(0);
        dealBill.setDealState(DealState.willing);
        dealBill.setDealTime(LocalDateTime.now());
        dealBill.setVersion(0);
        int insert = dealBillDao.insert(dealBill);
//        if (insert==0){
//            throw new RuntimeException("保存失败");
//        }
//
//        Player consumer = playerDao.findByCode(dealBill.getConsumerCode());
//        if (consumer==null){
//            throw new RuntimeException("消费者不存在");
//        }
//        int i = playerDao.updateGoldCoinByCode(dealBill.getConsumerCode(),
//                consumer.getGoldCoin() + dealBill.getQuantity());
//        Player supplier = playerDao.findByCode(dealBill.getSupplierCode());
//        if (supplier==null){
//            throw new RuntimeException("提供者不存在");
//        }
////        if(i==1){
////            throw  new RuntimeException("模拟不可预测的原因");
////        }
////        try {
////            Thread.sleep(25000);
////
////        } catch (InterruptedException e){
////
////            e.printStackTrace();
////        }
//        int i1 = playerDao.updateGoldCoinByCode(dealBill.getSupplierCode(),
//
//                supplier.getGoldCoin() - dealBill.getQuantity());
//        return (i>0&&i1>0);

//        Boolean deal = playerService.txDeal(dealBill);
//        return deal;
        Boolean r=null;
        try {
            r = playerService.txDeal(dealBill);
            if (r){
                dealBill.setDealState(DealState.succeeded);
            }
        }catch (Exception e){
            dealBill.setDealState(DealState.failed);
            throw new SeptemberException("交易事务异常");
        }finally {
            int i = dealBillDao.insert(dealBill);
        }
        return r;

    }


    @PostMapping("/query")
    public PagedResults<DealBill> query(
            @RequestBody DealBillSearchForm dealBillSearchForm){
        Integer pageNum = dealBillSearchForm.getPageNum();
        Integer pageSize = dealBillSearchForm.getPageSize();
        IPage<DealBill> page = new Page<>(pageNum, pageSize);
        //Ipage
        DealBillQueryCondition queryCondition = dealBillSearchForm.getQueryCondition();
        //多条件where
       // dealBillDao.dynamicQuery(page,queryCondition);
        IPage<DealBill> dynamicQuery = dealBillDao.dynamicQuery(page, queryCondition);
        //todo
        PagedResults<DealBill> pagedResults = new PagedResults();
        pagedResults.setResults(dynamicQuery.getRecords());
        pagedResults.setTotal(dynamicQuery.getTotal());
        return pagedResults;
        //return null;

    }


}
